import Vue from 'vue'
// import store from '../store/index'
import VueRouter from 'vue-router'
import Main from '../views/Main.vue'
import UserNew from '../views/User/UserNew.vue'
import UserList from '../views/User/UserList.vue'
import Login from '../views/Login.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: "/login", name: "Login", component: Login, meta: { ispublic: true }
  },
  {
    path: '/',
    name: 'Main',
    component: Main,
    children: [
      {
        path: "/User/UserNew",
        name: "UserNew",
        component: UserNew,
      },
      {
        path: "/User/UserList",
        name: "UserList",
        component: UserList,
      },
      {
        path: "/User/UserEdit/:id",
        name: "UserEdit",
        component: UserNew,
        props: true
      },
      {
        path: "/app/index/",
        component: () => import('../views/app/index.vue'),
        props: true
      }, {
        path: "/app/indexEadit/:id",
        component: () => import('../views/app/index.vue'),
        props: true
      }, {
        path: "/app/list",
        component: () => import('../views/app/list.vue'),
      },
      {
        path: "/noteMode/index/",
        component: () => import('../views/noteMode/index.vue'),
        props: true
      }, {
        path: "/noteMode/indexEadit/:id",
        component: () => import('../views/noteMode/index.vue'),
        props: true
      }, {
        path: "/noteMode/list",
        component: () => import('../views/noteMode/list.vue'),
      }
    ]
  }
]

const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  // base: `/app/`,
  routes
})

router.beforeEach((to, from, next) => {
  // console.log('to', to)
  if (!to.meta.ispublic && !sessionStorage.getItem('token')) {
    return next('/Login')
  }
  next()
})

const originalPush = VueRouter.prototype.push
//修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
   return originalPush.call(this, location).catch(err => err)
}
export default router
